package com.example.park2212.news

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.entity.NewsBeanItem
import com.example.lib_base.mvvm.BaseActivity
import com.example.park2212.R
import com.example.park2212.databinding.ActivityNewsAddBinding
import com.example.park2212.repair.RepairImgAdapter
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/news/NewsAddActivity")
class NewsAddActivity : BaseActivity<ActivityNewsAddBinding,NewsViewModel>() {
    var type = ""
    lateinit var repairImgAdapter: RepairImgAdapter
    override fun initView() {

        binding.newsAddHead.headerTitle.setText("发布新闻")
        binding.newsAddHead.headerBack.setOnClickListener {
            finish()
        }


        val list = arrayListOf<String>("园区新闻","园区公告","校园新闻")
        var adapter = ArrayAdapter(this,android.R.layout.simple_spinner_item,list)
        binding.newsAddSptype.adapter = adapter
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.newsAddSptype.onItemSelectedListener = object :OnItemSelectedListener{
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                type = list[p2]
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }

        }


        binding.btnFb.setOnClickListener {
            val etTitle = binding.newsAddEttitle.text.toString()
            val etPerson = binding.newsAddEtperson.text.toString()
            val etTime = binding.newsAddEttime.text.toString()
            val etContext = binding.newsAddEtcontext.text.toString()
            val beanItem = NewsBeanItem(
                true, etContext, 1, listOf(
                    "http://example.com/image1.jpg", "http://example.com/image2.jpg"
                ), etTime, true, etPerson, etTitle, type
            )
            viewModel.addNews(beanItem)

        }

        repairImgAdapter = RepairImgAdapter()
        binding.rvImg.adapter = repairImgAdapter
        binding.rvImg.layoutManager = LinearLayoutManager(this,RecyclerView.HORIZONTAL,false)

        binding.nwesAddPhots.setOnClickListener {
            PictureSelector.create(this,PictureSelector.SELECT_REQUEST_CODE).selectPicture()
        }

        binding.nwesAddTime.setOnClickListener {
            showDateDialog(binding.newsAddEttime)
        }

    }

    override fun initData() {
        viewModel.addNewsSuccess.observe(this){
            ToastUtils.showLong("发布成功")
            finish()
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK){
            val pictureBean = data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            repairImgAdapter.addData(pictureBean?.path!!)
            repairImgAdapter.notifyDataSetChanged()
        }
    }

}